
package com.kkd.kkdapp.view;

import java.text.DecimalFormat;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.util.AttributeSet;
import android.view.animation.AccelerateDecelerateInterpolator;
import android.widget.TextView;

public class CountView extends TextView {

	private DecimalFormat mDf;//"#,###.00"
    public CountView(Context context) {
        super(context);
    }

    // 动画时长 ms
    int number;

    public CountView(Context context, AttributeSet attrs) {
        super(context, attrs);
    }

    public void showNumberWithAnimation(int from, int add, int duration) {
        // 修改number属性，会调用setNumber方法
        ObjectAnimator objectAnimator = ObjectAnimator.ofInt(this, "number", from, from + add);
        objectAnimator.setDuration(duration);
        // 加速器，从慢到快到再到慢
        objectAnimator.setInterpolator(new AccelerateDecelerateInterpolator());
        objectAnimator.start();
    }

    public int getNumber() {
        return number;
    }

    public void setNumber(int num) {
        number = num;
        if(mDf!=null)
        	setText(mDf.format(number));
        else
        	setText(String.valueOf(number));
    }
    public void setNumber(String num) {
    	
        number = (int) Double.parseDouble(num);
        if(mDf!=null)
        	setText(mDf.format(number));
        else
        	setText(String.valueOf(number));
    }
    public void setDecimalFormat(DecimalFormat df){
    	mDf = df;
    }
}
